﻿using Comm;
using Kinlo.Model;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Kinlo.Repo.MySql
{
    public class CounterRep
    {
        string strConn;
        public CounterRep()
        {
            strConn = ConfigurationManager.ConnectionStrings["WeightDBMySql"].ConnectionString;
        }

        public int UpdateBackCounter(int outCount, int okCount, int ngCount)
        {
            try
            {
                using (var conn = new MySqlConnection(strConn))
                {

                    var cmd = string.Format(@"UPDATE  Counter  SET OutCount='{0}',OKCount='{1}',NGCount='{2}',LastTime=NOW()",
                    outCount, okCount, ngCount);
                    var sqlCmd = new MySqlCommand(cmd, conn);
                    conn.Open();
                    return sqlCmd.ExecuteNonQuery();
                }
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("{0} {1} {2}", "CounterRep", "UpdateBackCounter", ex);
                return 0;
            }
        }

        public int UpdateFrontCounter(int inCount)
        {
            try
            {
                using (var conn = new MySqlConnection(strConn))
                {

                    var cmd = string.Format(@"UPDATE Counter SET InCount='{0}',LastTime=NOW()", inCount);
                    var sqlCmd = new MySqlCommand(cmd, conn);
                    conn.Open();
                    return sqlCmd.ExecuteNonQuery();
                }
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("{0} {1} {2}", "CounterRep", "UpdateFrontCounter", ex);
                return 0;
            }
        }

        /// <summary>
        /// 清零记数器
        /// </summary>
        /// <param name="outCount"></param>
        /// <param name="okCount"></param>
        /// <param name="ngCount"></param>
        /// <returns></returns>
        public int ClearCounter()
        {
            try
            {
                using (var conn = new MySqlConnection(strConn))
                {
                    var cmd = string.Format(@"UPDATE Counter SET InCount='0',OutCount='0',OKCount='0',NGCount='0',LastTime=NOW()");
                    var sqlCmd = new MySqlCommand(cmd, conn);
                    conn.Open();
                    return sqlCmd.ExecuteNonQuery();
                }
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("{0} {1} {2}", "CounterRep", "ClearCounter", ex);
                return 0;
            }
        }

        public Counter GetCounter()
        {
            try
            {
                using (var conn = new MySqlConnection(strConn))
                {
                    var cmd = string.Format(@"SELECT  * FROM Counter ORDER BY LastTime desc  LIMIT 1 ");
                    var sqlCmd = new MySqlCommand(cmd, conn);
                    conn.Open();
                    var reader = sqlCmd.ExecuteReader();
                    return reader.ReaderToModel<Counter>();
                }
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("{0} {1} {2}", "CounterRep", "GetCounter", ex);
                return null;
            }
        }
        public Counter GetCounter2()
        {
            try
            {
                using (var conn = new MySqlConnection(strConn))
                {
                    var cmd = string.Format(@"SELECT  ID,InCount,OutCount,OKCount,NGCount,CurrentTime LastTime FROM CounterBak  ORDER BY CurrentTime desc  LIMIT 1 ");
                    var sqlCmd = new MySqlCommand(cmd, conn);
                    conn.Open();
                    var reader = sqlCmd.ExecuteReader();
                    return reader.ReaderToModel<Counter>();
                }
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("{0} {1} {2}", "CounterRep", "GetCounter", ex);
                return null;
            }
        }
        public bool GetDatabaseStatus()
        {
            try
            {
                using (var conn = new MySqlConnection(strConn))
                {
                    var cmd = string.Format(@"SELECT 1");
                    var sqlCmd = new MySqlCommand(cmd, conn);
                    conn.Open();
                    var reader = sqlCmd.ExecuteReader();
                    return true;
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        public int InsertCounterBak()
        {
            try
            {
                using (var conn = new MySqlConnection(strConn))
                {
                    ///最后时间
                    var c = GetCounter();
                    var c1 = GetCounter2();
                    var cmd = string.Format(@"INSERT CounterBak( InCount ,OutCount ,OKCount ,NGCount ,LastTime,CurrentTime,BatchNo)
                    VALUES('{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}')", c.InCount, c.OutCount, c.OKCount, c.NGCount,
                    c1==null?DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") : c1.LastTime.ToString("yyyy-MM-dd HH:mm:ss"), DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), GlobalValues.BatchNo);
                    var sqlCmd = new MySqlCommand(cmd, conn);
                    conn.Open();
                    return sqlCmd.ExecuteNonQuery();
                }
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("{0} {1} {2}", "CounterRep", "InsertCounterBak", ex);
                return 0;
            }
        }
    }

}
